/**
 * Copyright (C), 2018 store
 * Encoding: UTF-8
 * Date: 20-4-15 下午12:32
 * History:
 */
package com.swak.frame.config;

import java.util.List;

import javax.servlet.ServletContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import com.google.common.collect.Lists;
import com.swak.frame.shrio.tag.ShiroTags;

import freemarker.template.TemplateModelException;
import freemarker.template.utility.XmlEscape;

/**
 * SwakWebMvcConfigurer.java
 * 
 * @author ColleyMa
 * @date 20-4-15 下午12:32
 * @version v1.0
 */
public  class SwakWebMvcConfigurer implements SwakMvcConfigurer {

	@Autowired(required = false)
	private freemarker.template.Configuration configuration;

	@Autowired
	private ServletContext servletContext;

	@Override
	public void configureSwakResolver(ViewResolverRegistry registry) {
		if (configuration != null) {
			// 自定义配置信息
			try {
				this.configuration.setSharedVariable("ctx", this.servletContext.getContextPath());
				this.configuration.setSharedVariable("xml_escape", new XmlEscape());
				this.configuration.setSharedVariable("shiro", new ShiroTags());
			} catch (TemplateModelException e) {
				// ingro
			}
		}
	}

	@Override
	public void configureViewResolvers(ViewResolverRegistry registry) {
		configureSwakResolver(registry);
	}

	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
		registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
	}

	/**
	 * 跨域设置
	 */
	@Override
	public void addCorsMappings(CorsRegistry registry) {
		registry.addMapping("/**") // 允许跨域访问的路径
				.allowedOriginPatterns("*")
				.allowedMethods("POST", "GET", "PUT", "OPTIONS", "DELETE") // 允许请求方法
				.maxAge(1800) // 预检间隔时间
				.allowedHeaders("*") // 允许头部设置
				.allowCredentials(true); // 是否发送cookie
	}

	@Override
	public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
		for (HttpMessageConverter<?> messageConverter : converters) {
			System.out.println(messageConverter);
		}
	}

	@Override
	public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
		FastJsonHttpMessageConverter convert = new FastJsonHttpMessageConverter();
		convert.setSupportedMediaTypes(
				Lists.newArrayList(MediaType.APPLICATION_JSON, MediaType.TEXT_HTML, MediaType.TEXT_PLAIN));
		FastJsonConfig config = new FastJsonConfig();
		config.setSerializerFeatures(SerializerFeature.DisableCircularReferenceDetect);
		convert.setFastJsonConfig(config);
		converters.add(0, convert);
	}
}
